﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
using Web.ChartReport.DataSet;
using System.Data;
using Microsoft.Reporting.WebForms;

namespace Web.ChartReport.Student
{
    public partial class StudentSynthesizeWebForm : System.Web.UI.Page
    {
        protected string ViewMyScore = "";
        protected string ViewClassEverage = "", ViewGClassMax = "", ViewClassNumber = "";
        protected string ViewGradeEverage = "", ViewGradeMax = "", ViewGradeNumber = "";
        protected string ViewAreaEverage = "", ViewAreaMax = "", ViewAreaNumber = "";
        protected int examroomid = 0, studentid = 0;
        protected string ChartString = "";
        protected string bcnm = "";
        protected string comment = "";
        protected bool isquery = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "StatistScore"))
            {
                if (!UserValidate.CheckQuery())
                {
                    Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                    Response.Redirect("login.aspx");
                    Response.End();
                }
                else
                {
                    isquery = true;
                }
            }
            if (isquery)
            {
                studentid = int.Parse(HttpContext.Current.Session["QueryStudentId"] as string);
            }
            else if (!PageValidate.QueryStringIsEmpty("studentid"))
            {
                try
                {
                    studentid = int.Parse(PageValidate.QueryString("studentid"));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            if (!PageValidate.QueryStringIsEmpty("examroomid"))
            {
                try
                {
                    examroomid = int.Parse(PageValidate.QueryString("examroomid"));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            if (!IsPostBack)
            {
                if (studentid > 0 && examroomid > 0)
                {
                    if (ExamRoom.CheckObjectById(examroomid))
                    {
                        ExamRoomInfo examroomInfo = ExamRoom.GetSingleInfo(examroomid);
                        comment = examroomInfo.Comment;
                        IList<StudentScoreExtendInfo> infos = new List<StudentScoreExtendInfo>();
                        infos = StudentScoreExtend.GetList(examroomInfo.Id, 0, 0, 0, 0, 0, 0, 0);
                        StudentScoreExtendInfo myInfo = infos.Single(t => t.StudentId == studentid);
                        ViewMyScore = myInfo.CountScore.ToString();
                        ClassCount(infos, myInfo.ClassId);
                        GradeCount(infos, myInfo.GradeId);
                        AreaCount(infos, myInfo.AreaId);
                        if (myInfo.CountScore >= 85)
                        {
                            bcnm = "<span class='face_a' title='优'>优</span>【优】";
                        }
                        else if (myInfo.CountScore >= 70)
                        {
                            bcnm = "<span class='face_b' title='良'>良</span>【良】";
                        }
                        else if (myInfo.CountScore >= 60)
                        {
                            bcnm = "<span class='face_c' title='中'>中</span>【中】";
                        }
                        else if (myInfo.CountScore < 60)
                        {
                            bcnm = "<span class='face_d' title='差'>差</span>【差】";
                        }

                        var studentInfoTable = new StudentScoreDataSet.StudentInfoDataTable();
                        DataRow row0 = studentInfoTable.NewRow();
                        row0[0] = myInfo.Name;
                        row0[1] = "111222333444";
                        row0[2] = "s200999999";
                        studentInfoTable.Rows.Add(row0);
                        ReportDataSource rds1 = new ReportDataSource("StudentInfo", studentInfoTable as DataTable);

                        var studentScoreTable = new StudentScoreDataSet.StudentScoreTableDataTable();
                        DataRow row = studentScoreTable.NewRow();
                        row[0] = ViewMyScore;
                        row[1] = ViewClassEverage;
                        row[2] = ViewGClassMax;
                        row[3] = ViewClassNumber;
                        row[4] = ViewGradeEverage;
                        row[5] = ViewGradeMax;
                        row[6] = ViewGradeNumber;
                        row[7] = ViewAreaEverage;
                        row[8] = ViewAreaMax;
                        row[9] = ViewAreaNumber;
                        studentScoreTable.Rows.Add(row);
                        ReportDataSource rds2 = new ReportDataSource("StudentScoreTable", studentScoreTable as DataTable);

                        var averageScoreTable = new StudentScoreDataSet.AverageScoreTableDataTable();
                        DataRow row1 = averageScoreTable.NewRow();
                        row1[0] = "个人";
                        row1[1] = ViewMyScore;
                        averageScoreTable.Rows.Add(row1);
                        DataRow row2 = averageScoreTable.NewRow();
                        row2[0] = "班级";
                        row2[1] = ViewClassEverage;
                        averageScoreTable.Rows.Add(row2);
                        DataRow row3 = averageScoreTable.NewRow();
                        row3[0] = "年级";
                        row3[1] = ViewGradeEverage;
                        averageScoreTable.Rows.Add(row3);
                        DataRow row4 = averageScoreTable.NewRow();
                        row4[0] = "学区";
                        row4[1] = ViewAreaEverage;
                        averageScoreTable.Rows.Add(row4);
                        ReportDataSource rds3 = new ReportDataSource("AverageScoreTable", averageScoreTable as DataTable);


                        var evaluationDataTable = new StudentScoreDataSet.EvaluationDataTable();
                        DataRow row5 = evaluationDataTable.NewRow();
                        row5[0] = "你成绩为89，比班级平均成绩低2分。";
                        evaluationDataTable.Rows.Add(row5);
                        ReportDataSource rds4 = new ReportDataSource("Evaluation", evaluationDataTable as DataTable);

                        var knowledgeAreaScoreDataTable = new StudentScoreDataSet.KnowledgeAreaScoreDataTable();
                        for (int i = 0; i < 40; i++)
                        {
                            DataRow row6 = knowledgeAreaScoreDataTable.NewRow();
                            row6[0] = i + 1;
                            row6[1] = "数与代数";
                            row6[2] = "10";
                            row6[3] = "46";
                            row6[4] = "39";
                            row6[5] = "83%";
                            row6[6] = "84%";
                            row6[7] = "65%";
                            row6[8] = "45%";
                            knowledgeAreaScoreDataTable.Rows.Add(row6);
                        }
                        ReportDataSource rds5 = new ReportDataSource("KnowledgeAreaScore", knowledgeAreaScoreDataTable as DataTable);

                        ReportViewer1.ProcessingMode = ProcessingMode.Local;
                        LocalReport localReport = ReportViewer1.LocalReport;
                        localReport.DataSources.Clear();
                        string basePath = AppDomain.CurrentDomain.BaseDirectory;
                        localReport.ReportPath = basePath + @"ChartReport\Student\StudentSynthesizeReport.rdlc";
                        localReport.DataSources.Add(rds1);
                        localReport.DataSources.Add(rds2);
                        localReport.DataSources.Add(rds3);
                        localReport.DataSources.Add(rds4);
                        localReport.DataSources.Add(rds5);
                        localReport.Refresh();
                    }
                }
            }
        }

        private void ClassCount(IList<StudentScoreExtendInfo> infos, int classid)
        {
            decimal count = infos.Where(t => t.ClassId == classid).Sum(t => t.CountScore);
            StudentScoreExtendInfo info = infos.Where(t => t.ClassId == classid).OrderByDescending(t => t.CountScore).First();
            int rencount = infos.Where(t => t.ClassId == classid).Count();
            ViewClassEverage = Math.Round((count / rencount), 1).ToString();
            ViewClassNumber = rencount.ToString();
            ViewGClassMax = info.CountScore.ToString();

        }

        private void GradeCount(IList<StudentScoreExtendInfo> infos, int gradeid)
        {
            decimal count = infos.Where(t => t.GradeId == gradeid).Sum(t => t.CountScore);
            StudentScoreExtendInfo info = infos.Where(t => t.GradeId == gradeid).OrderByDescending(t => t.CountScore).First();
            int rencount = infos.Where(t => t.GradeId == gradeid).Count();
            ViewGradeEverage = Math.Round((count / rencount), 1).ToString();
            ViewGradeNumber = rencount.ToString();
            ViewGradeMax = info.CountScore.ToString();
        }

        private void AreaCount(IList<StudentScoreExtendInfo> infos, int areaid)
        {
            decimal count = infos.Where(t => t.AreaId == areaid).Sum(t => t.CountScore);
            StudentScoreExtendInfo info = infos.Where(t => t.AreaId == areaid).OrderByDescending(t => t.CountScore).First();
            int rencount = infos.Where(t => t.AreaId == areaid).Count();
            ViewAreaEverage = Math.Round((count / rencount), 1).ToString();
            ViewAreaMax = info.CountScore.ToString();
            ViewAreaNumber = rencount.ToString();
        }
    }
}